local skel = fk.CreateSkill {
  name = "rmt__shuntian",
  tags = {Skill.Compulsory},
}

skel:addEffect(fk.Damaged, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(skel.name) then return false end
    return not target.dead and target ~= player and data.from == player
  end,
  on_cost = function (self, event, target, player, data)
    event:setCostData(self, {tos = {data.to} })
    return true
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    if (not target:isWounded()) or room:askToChoice(target, {choices = {"draw2", "recover"}, skill_name = skel.name}) == "draw2" then
      target:drawCards(2, skel.name)
    else
      room:recover { num = 1, skillName = skel.name, who = target, recoverBy = player }
    end
  end,
})

skel:addEffect(fk.TargetSpecified, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(skel.name) then return false end
    local to = data.to
    if target == player and to and not to.dead and not to:isNude() and to ~= player and data.card.trueName == "slash" then
      return data.extra_data and data.extra_data.rmt__shuntianSlash
    end
  end,
  on_cost = function (self, event, target, player, data)
    event:setCostData(self, {tos = {data.to} })
    return true
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local to = data.to
    local cards = to:getCardIds("he")
    if #cards > 1 then
      cards = room:askToCards(to, {
        min_num = 1, max_num = 1, skill_name = skel.name, include_equip = true, cancelable = false,
        prompt = "#rmt__shuntian-give:"..player.id
      })
    end
    room:obtainCard(player, cards, false, fk.ReasonGive, to, skel.name)
  end,
})

skel:addEffect(fk.DamageCaused, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(skel.name) then return false end
    return target == player and #player:getTableMark("@rmt__shuntian") == 4
  end,
  on_cost = function (self, event, target, player, data)
    event:setCostData(self, {tos = {data.to} })
    return true
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "@rmt__shuntian", 0)
    local damage = data.to.hp + data.to.shield
    if damage > data.damage then
      data:changeDamage(damage - data.damage)
    end
  end,
})

skel:addEffect(fk.CardUsing, {
  can_refresh = function(self, event, target, player, data)
    return target == player and data.card.trueName == "slash" and player:getMark("rmt__shuntian_used-turn") == 0
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "rmt__shuntian_used-turn", 1)
    if player:hasSkill(skel.name) then
      data.extra_data = data.extra_data or {}
      data.extra_data.rmt__shuntianSlash = true
    end
  end,
})

skel:addEffect(fk.AfterCardsMove, {
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and #player:getTableMark("@rmt__shuntian") < 4
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    local oldmark = player:getTableMark("@rmt__shuntian")
    local mark = table.simpleClone(oldmark)
    for _, move in ipairs(data) do
      if move.from == player  then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerEquip or info.fromArea == Card.PlayerHand then
            if Fk:getCardById(info.cardId).suit ~= Card.NoSuit then
              table.insertIfNeed(mark, Fk:getCardById(info.cardId):getSuitString(true))
            end
          end
        end
      end
    end
    if #mark > #oldmark then
      room:setPlayerMark(player, "@rmt__shuntian", mark)
    end
  end,
})

skel:addEffect("targetmod", {
  bypass_distances = function(self, player, skill, card, to)
    return card and card.trueName == "slash" and player:hasSkill(skel.name) and player:getMark("rmt__shuntian_used-turn") == 0
  end,
  extra_target_func = function(self, player, skill)
    if skill and skill.trueName == "slash_skill" and player:hasSkill(skel.name) and player:getMark("rmt__shuntian_used-turn") == 0 then
      return 999
    end
  end,
})

Fk:loadTranslationTable{
  ["rmt__shuntian"] = "顺天",
  [":rmt__shuntian"] = "锁定技，你每回合首张【杀】无距离和目标上限限制，且目标须交给你一张牌；其他角色受到你的伤害后，须选择回复1点体力或摸两张牌 ；你每失去四种花色的牌后，下次造成的伤害将致命。 ",

  ["#rmt__shuntian-give"] = "顺天：请交给 %src 一张牌",
  ["@rmt__shuntian"] = "顺天",
}

return skel
